﻿namespace BloodlustFury.DataMiner
{
	#region "Using Statements"

	using System;
	using System.Windows.Forms;
	using BloodlustFury.DataMiner.Business;

	#endregion

	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void caseLibraryToolStripMenuItem_Click(object sender, EventArgs e)
		{
			CaseLibraryForm caseLibForm = new CaseLibraryForm();

			caseLibForm.MdiParent = this;
			caseLibForm.Show();
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			DialogResult result = MessageBox.Show("Are you sure you wish to exit?", "Bloodlust Fury - DataMiner", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

			if (result != DialogResult.Yes)
			{
				e.Cancel = true;
			}
		}

		private void testModulesToolStripMenuItem_Click(object sender, EventArgs e)
		{
			TestDataForm testDataForm = new TestDataForm();

			testDataForm.MdiParent = this;
			testDataForm.Show();
		}

		private void loadTestDataToolStripMenuItem_Click(object sender, EventArgs e)
		{
			DialogResult result = DialogResult.Yes;

			if (DataEngine.TestModulesLoaded)
			{
				result = MessageBox.Show("Loading a new file will delete any existing data you have already generated.\nDo you wish to continue?",
																 "Bloodlust Fury - DataMiner",
																 MessageBoxButtons.YesNo,
																 MessageBoxIcon.Question);
			}

			if (result == DialogResult.Yes)
			{
				if (BrowseFileDialog.ShowDialog() == DialogResult.OK)
				{
					try
					{
						DataEngine.LoadTestModules(BrowseFileDialog.FileName);
						DataEngine.ClearStatistics();
						DataEngine.PredictionsGenerated = false;
					}
					catch (Exception ex)
					{
						MessageBox.Show(String.Format("Error loading test modules:\n{0}", ex.Message),
														"Bloodlust Fury - DataMiner",
														MessageBoxButtons.OK,
														MessageBoxIcon.Error);

						return;
					}

					MessageBox.Show("Test Modules have been loaded.", "Bloodlust Fury - DataMiner", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
		}

		private void loadCaseLibraryToolStripMenuItem_Click(object sender, EventArgs e)
		{
			DialogResult result = DialogResult.Yes;

			if (DataEngine.TestModulesLoaded && DataEngine.CaseLibraryLoaded)
			{
				result = MessageBox.Show("Loading a new file will delete any existing data you have already generated.\nDo you wish to continue?",
																 "Bloodlust Fury - DataMiner",
																 MessageBoxButtons.YesNo,
																 MessageBoxIcon.Question);
			}

			if (result == DialogResult.Yes)
			{
				if (BrowseFileDialog.ShowDialog() == DialogResult.OK)
				{
					try
					{
						DataEngine.LoadCaseLibrary(BrowseFileDialog.FileName);
						DataEngine.ClearPredictions();
						DataEngine.ClearStatistics();
					}
					catch (Exception ex)
					{
						MessageBox.Show(String.Format("Error loading case library:\n{0}", ex.Message),
														"Bloodlust Fury - DataMiner",
														MessageBoxButtons.OK,
														MessageBoxIcon.Error);

						return;
					}

					MessageBox.Show("Case Library has been loaded.", "Bloodlust Fury - DataMiner", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
		}

		private void loadSolutionsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			DialogResult result = DialogResult.Yes;

			if (DataEngine.SolutionsLoaded)
			{
				result = MessageBox.Show("Loading a new file will delete any existing data you have already generated.\nDo you wish to continue?",
																 "Bloodlust Fury - DataMiner",
																 MessageBoxButtons.YesNo,
																 MessageBoxIcon.Question);
			}

			if (result == DialogResult.Yes)
			{
				if (BrowseFileDialog.ShowDialog() == DialogResult.OK)
				{
					try
					{
						DataEngine.LoadSolutionData(BrowseFileDialog.FileName);
						DataEngine.ClearStatistics();
					}
					catch (Exception ex)
					{
						MessageBox.Show(String.Format("Error loading solution data:\n{0}", ex.Message), 
														"Bloodlust Fury - DataMiner", 
														MessageBoxButtons.OK, 
														MessageBoxIcon.Error);

						return;
					}

					MessageBox.Show("Solution data has been loaded.", "Bloodlust Fury - DataMiner", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
		}

		private void statisticsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			StatisticsForm statsForm = new StatisticsForm();

			statsForm.MdiParent = this;
			statsForm.Show();
		}

		private void generatePredictionsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (DataEngine.TestModulesLoaded && DataEngine.CaseLibraryLoaded)
			{
				NearestNeighborDialogBox nearestNeighbors = new NearestNeighborDialogBox();

				if (nearestNeighbors.ShowDialog() == DialogResult.OK)
				{
					Cursor = Cursors.WaitCursor;

					DataEngine.FindNearestNeighbor(nearestNeighbors.NearestNeighbors);
					DataEngine.GeneratePredictions();

					Cursor = Cursors.Default;

					MessageBox.Show("Predictions have been generated.",
													"Bloodlust Fury - DataMiner",
													MessageBoxButtons.OK,
													MessageBoxIcon.Information);
				}
			}
			else
			{
				MessageBox.Show("You must load both a Case Library and Test Modules in order to generate predictions.",
												"Bloodlust Fury - DataMiner",
												MessageBoxButtons.OK,
												MessageBoxIcon.Information);

			}
		}

		private void generateStatisticsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (!DataEngine.StatisticsGenerated)
			{
				if (DataEngine.PredictionsGenerated
						&& DataEngine.TestModulesLoaded
						&& DataEngine.CaseLibraryLoaded
						&& DataEngine.SolutionsLoaded)
				{
					Cursor = Cursors.WaitCursor;

					DataEngine.GenerateStatistics();

					Cursor = Cursors.Default;

					MessageBox.Show("Statistics have been generated.",
													"Bloodlust Fury - DataMiner",
													MessageBoxButtons.OK,
													MessageBoxIcon.Information);
				}
				else
				{
					MessageBox.Show("You must first generate predictions and load solution data in order to generate statistics.",
												"Bloodlust Fury - DataMiner",
												MessageBoxButtons.OK,
												MessageBoxIcon.Information);
				}
			}
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AboutForm aboutForm = new AboutForm();

			aboutForm.MdiParent = this;
			aboutForm.Show();
		}
	}
}
